package cn.sccl.dirms.monitor.controller;

import cn.sccl.common.zabbix.Zabbix;
import cn.sccl.dirms.monitor.model.ImageExtend;
import cn.sccl.dirms.monitor.model.TemplateAndIcons;
import cn.sccl.dirms.monitor.service.HostGroupManager;
import cn.sccl.dirms.monitor.service.ImageManager;
import cn.sccl.dirms.monitor.service.TemplateAndIconsManager;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模板
 * Created by liupan on 2017/3/6.
 */
@RestController
@RequestMapping(value = "/dirms/monitor/template")
public class MonitorTemplateController {


    @Autowired
    private HostGroupManager hostGroupManager ;

    @Autowired
    private ImageManager imageManger ;


    @Autowired
    private TemplateAndIconsManager templateAndIconsManager;

    /**
     * 获取所有模板
     */
    @RequestMapping(value = "/all", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String,Object> listTemplate() {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output","extend");
        return zabbix.call("template.get",params);
    }


    /**
     * 获取所有模板
     */
    @RequestMapping(value = "/all/templateWithImage", method = {RequestMethod.POST,RequestMethod.GET})
    public List<Map<String,Object>> listTemplateWithImageData() {
        //找到所有群组分组（有模板的群组）
        List<Map<String,Object>> templatesGroup=hostGroupManager.listHostgroup();
        //所有模板和图标的关系
        List<TemplateAndIcons> templateAndIconses = templateAndIconsManager.listAll();
        //获取到所有的图标
        List<ImageExtend>   imageExtends= imageManger.listAll();

        // TODO: 2017/6/5  模板，图标，关系，三者齐聚，开工
        for (Map<String, Object> stringObjectMap : templatesGroup) {
           if (stringObjectMap.containsKey("templates")){
               List<Object> objectList =( List<Object>  )stringObjectMap.get("templates");
               for (Object o : objectList) {
                   Map<String,Object> template =(Map<String,Object>) o;
                   String templateId =(String) template.get("templateid") ;
                   for (TemplateAndIcons templateAndIconse : templateAndIconses) {
                       // TODO: 2017/6/5 在关系 中查询该模板对应的imageId
                       if (templateAndIconse.getTemplateId().equals(templateId)){
                           ((Map<String, Object>) o).put("reTemplateAndIcons",templateAndIconse);
                           String imageId = templateAndIconse.getIconsId() ;
                           for (ImageExtend imageExtend : imageExtends) {
                               if (imageExtend.getImageid().equals(imageId)){
                                   ((Map<String, Object>) o).put("iconData",imageExtend);
                                   break;
                               }
                           }
                       }

                   }

               }
           }

        }
        return templatesGroup ;
    }



    /**
     * 根据名称模糊查询模板
     * @param name 名称
     */
    @RequestMapping(value = "/queryByName/{name}", method = {RequestMethod.POST,RequestMethod.GET})
    public List<Map<String,Object>> queryByName(@PathVariable String name) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output","extend");
        JSONObject temp = zabbix.call("template.get",params);
        List<Map<String, Object>> result = (List<Map<String, Object>>) temp.get("result");
        List<Map<String, Object>> newReslt = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> item: result) {
            String itemName = (String) item.get("name");
            if (itemName.contains(name)) {
                newReslt.add(item);
            }
        }
        return newReslt;
    }

    /**
     * 根据组id查询模板
     * @param groupid 组id
     */
    @RequestMapping(value = "/queryByGroupId/{groupid}", method = {RequestMethod.POST,RequestMethod.GET})
    public Map<String,Object> queryByHostGroup(@PathVariable String groupid) {
        Zabbix zabbix = Zabbix.getZabbix();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("output","extend");
        params.put("groupids",groupid);
        return zabbix.call("template.get",params);
    }

}
